from typing import Optional

class ListNode:
    def __init__(self,val=0,next=None):
        self.val = val
        self.next = next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode])->Optional[ListNode]:
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            carry+=(l1.val if l1 else 0)+(l2.val if l2 else 0)
            cur.next = ListNode(carry%10)
            carry//=10
            cur = cur.next
            if l1:l1=l1.next
            if l2:l2=l2.next
        return dummy.next



from typing import  List
from typing import  Optional
class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode])->Optional[ListNode]:#语法：Optional[Type] 等价于 Union[Type, None]。
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            carry+=(l1.val if l1 else 0)+(l2.val if l2 else 0)
            cur.next=(carry%10)
            carry //=10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+ carry
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

    class Solution:
        def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]):
            cur = dummy = ListNode()
            carry = 0
            while l1 or l2 or carry:
                total = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
                cur.next = ListNode(total % 10)
                carry = total // 10
                cur = cur.next
                if l1:
                    l1 = l1.next
                if l2:
                    l2 = l2.next
            return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l2 or l1 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode])->ListNode:
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total //10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next

        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next

class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next

        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next

class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next

        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode])->Optional[ListNode]:
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next

class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode])->ListNode:
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next
class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1 :
                l1=l1.next
            if l2:
                l2=l2.next

        return dummy.next
class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addtwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1 :
                l1=l1.next
            if l2 :
                l2=l2.next
        return dummy.next













